package com.bobo.rbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bobo.rbac.domain.User;
import com.bobo.rbac.domain.UserRole;
import com.bobo.rbac.service.UserRoleService;
import com.bobo.rbac.service.UserService;
import com.bobo.rbac.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* @author bobo
* @description 针对表【user】的数据库操作Service实现
* @createDate 2022-07-27 14:42:55
*/
@Transactional
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Autowired
    UserRoleService userRoleService;
    @Autowired
    UserMapper userMapper;

    @Override
    public boolean bindRole(User user) {
        //1先删除用户角色中间表
        QueryWrapper<UserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",user.getId());
        boolean b = userRoleService.remove(wrapper);
        //2。再次添加用户中间表
        List<UserRole> list = new ArrayList<>();

        if( user.getRoleIds()!=null) {
       /*     for (Integer roleId : user.getRoleIds()) {
                UserRole userRole = new UserRole();
                userRole.setRid(roleId);
                userRole.setUid(user.getId());
                list.add(userRole);
            }*/


            user.getRoleIds().forEach(rid->{
                UserRole userRole = new UserRole();
                userRole.setRid(rid);
                userRole.setUid(user.getId());
                list.add(userRole);
            });

        }
        return userRoleService.saveBatch(list);//批量添加

    }


}




